//
//  STDCreditManager.m
//  FrontierApp
//
//  Created by curer on 11/13/15.
//  Copyright © 2015 lion. All rights reserved.
//

#import "STDCreditManager.h"

#define STDCreditManagerDefineTokenParam    @{@"token" : [STDCreditManager token]}

#define STDCreditManagerLogin             @"api/user/login/"
#define STDCreditManagerTaskList          @"api/task"
#define STDCreditManagerTaskCreate        @"api/task/create/"
#define STDCreditManagerTaskDelete        @"api/task/delete/"
#define STDCreditManagerTaskAccomplish    @"api/task/accomplish/"

NSString *kSTDCreditManagerNotificationLoginIn = @"STDCreditManagerNotificationLoginIn";
NSString *kSTDCreditManagerErrorDomain = @"STDCreditManager";

@interface STDCreditManager ()

@property (nonatomic, strong) AFHTTPRequestOperationManager *manager;
@property (nonatomic, strong) NSString *token;

@end

@implementation STDCreditManager

+ (instancetype)sharedInstance {
    static STDCreditManager *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[STDCreditManager alloc] init];
        NSURL *baseUrl = [NSURL URLWithString:@"http://192.168.99.175:3000/"];
        
        if (TARGET_IPHONE_SIMULATOR) {
            baseUrl = [NSURL URLWithString:@"http://localhost:3000/"];
        }
        
        //NSURL *baseUrl = [NSURL URLWithString:@"http://frontier.avosapps.com/"];
        
        _sharedInstance.manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseUrl];
        _sharedInstance.manager.responseSerializer = [AFJSONResponseSerializer serializer];
    });
    
    return _sharedInstance;
}

+ (AFHTTPRequestOperationManager *)manager {
    return [STDCreditManager sharedInstance].manager;
}

+ (void)setToken:(NSString *)token {
    NSAssert(token.length > 0, @"token must not be nill");
    [STDCreditManager sharedInstance].token = token;
}

+ (NSString *)token {
    return [STDCreditManager sharedInstance].token;
}

- (void)fetchTasks:(STDCreditManagerCallback)callback {
    [[STDCreditManager manager] GET:STDCreditManagerTaskList
                         parameters:STDCreditManagerDefineTokenParam
                            success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
                                
                                NSArray *datalist = [STDTaskModel arrayOfModelsFromDictionaries:responseObject[@"data"]];
                                if (callback) {
                                    callback(datalist, nil);
                                }
                                
                            } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
                                if (callback) {
                                    callback(nil, error);
                                }
                            }];
}

- (void)createTask:(STDTaskModel *)model callback:(STDCreditManagerCallback)callback {
    [[STDCreditManager manager] POST:[STDCreditManagerTaskCreate stringByAppendingString:model.objectId]
                          parameters:STDCreditManagerDefineTokenParam
                             success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
                                 if ([responseObject[@"error_code"] intValue] != 0) {
                                     if (callback) {
                                         callback(nil, [NSError errorWithDomain:@"STDCreditManager"
                                                                           code:[responseObject[@"message"] integerValue]
                                                                       userInfo:responseObject]);
                                     }
                                     return;
                                 }
                                 if (callback) {
                                     callback(responseObject, nil);
                                 }
                             } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
                                 if (callback) {
                                     callback(nil, error);
                                 }
                             }];
}

- (void)accomplishtTask:(STDTaskModel *)model callback:(STDCreditManagerCallback)callback {
    [[STDCreditManager manager] POST:[STDCreditManagerTaskAccomplish stringByAppendingString:model.objectId]
                    parameters:STDCreditManagerDefineTokenParam
                       success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
                           if ([responseObject[@"error_code"] intValue] != 0) {
                               if (callback) {
                                   callback(nil, [NSError errorWithDomain:@"STDCreditManager"
                                                                     code:[responseObject[@"message"] integerValue]
                                                                 userInfo:responseObject]);
                               }
                               return;
                           }
                           if (callback) {
                               callback(responseObject, nil);
                           }
                       } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
                           if (callback) {
                               callback(nil, error);
                           }
                       }];
}

- (void)cancelTask:(STDTaskModel *)model callback:(STDCreditManagerCallback)callback {
    [[STDCreditManager manager] POST:[STDCreditManagerTaskDelete stringByAppendingString:model.objectId]
                          parameters:STDCreditManagerDefineTokenParam
                             success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
                                 if ([responseObject[@"error_code"] intValue] != 0) {
                                     if (callback) {
                                         callback(nil, [NSError errorWithDomain:kSTDCreditManagerErrorDomain
                                                                           code:[responseObject[@"message"] integerValue]
                                                                       userInfo:responseObject]);
                                     }
                                     return;
                                 }
                                 if (callback) {
                                     callback(responseObject, nil);
                                 }
                       } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
                           if (callback) {
                               callback(nil, error);
                           }
                       }];
}

@end
